package com.jack.blog.controller;

import com.jack.blog.cache.UserServiceCache;
import com.jack.blog.vo.info.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user/{uid}")
public class UserController {

    @Autowired
    private UserServiceCache userServiceCache;

    @PostMapping("/update")
    public String updateUser(@PathVariable("uid")int uid, UserInfo userInfo, Model model){
        String url="/user/"+uid;
        try {
            userServiceCache.updateUserInfo(uid,userInfo);
            return "redirect:"+url;
        }catch (IllegalArgumentException i){
            model.addAttribute("error",i.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("error","update error!");
        }
        model.addAttribute("backUrl",url);
        return "error";
    }

}
